package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;

public class CommonDialog extends Dialog {

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    public CommonDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDialogWidth();
    }

    private void initDialogWidth() {
        Window window = getWindow();
        if (window == null) {
            return;
        }
        window.setDimAmount(0.6f); // 部分刷机会导致背景透明，这里保证一次
    }

    public static class Builder {

        private Context context;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private int positiveButtonColor;
        private int negativeButtonColor;
        private OnClickListener positiveListener;
        private OnClickListener negativeListener;


        private boolean canCancelable = true;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setPositiveButton(String text, OnClickListener listener) {
            this.positiveButtonText = text;
            this.positiveListener = listener;
            return this;
        }

        public Builder setNegativeButton(String text, OnClickListener listener) {
            this.negativeButtonText = text;
            this.negativeListener = listener;
            return this;
        }

        public Builder setCancelable(boolean canCancelable) {
            this.canCancelable = canCancelable;
            return this;
        }

        public Builder setPositiveButtonColor(int positiveButtonColor) {
            this.positiveButtonColor = positiveButtonColor;
            return this;
        }

        public Builder setNegativeButtonColor(int negativeButtonColor) {
            this.negativeButtonColor = negativeButtonColor;
            return this;
        }

        public CommonDialog create() {
            final CommonDialog commonDialog = new CommonDialog(context, R.style.dialog);
            View layout = null;
            layout = LayoutInflater.from(context).inflate(R.layout.dialog_commmon, null);

            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (CommonUtils.getScreenSize(context)[0] * 0.65f), ViewGroup.LayoutParams.WRAP_CONTENT);
            commonDialog.setContentView(layout, params);

            //findViews
            TextView tv_content = (TextView) layout.findViewById(R.id.tv_dialog_content);
            View viewLine = layout.findViewById(R.id.view_line);

            TextView btnPositive = (TextView) layout.findViewById(R.id.tv_dialog_positive);
            TextView btnNegative = (TextView) layout.findViewById(R.id.tv_dialog_negative);
            //setData
            if (TextUtils.isEmpty(message)) {
                tv_content.setVisibility(View.GONE);
            } else {
                tv_content.setText(message);
            }
            //bindListener
            if (!TextUtils.isEmpty(positiveButtonText) && positiveListener != null) {
                btnPositive.setText(positiveButtonText);
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        positiveListener.onClick(commonDialog, DialogInterface.BUTTON_POSITIVE);
                    }
                });
            } else {
                btnPositive.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(negativeButtonText) && negativeListener != null) {
                btnNegative.setText(negativeButtonText);
                btnNegative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        negativeListener.onClick(commonDialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            } else {
                btnNegative.setVisibility(View.GONE);
            }

            if (btnNegative.getVisibility() == View.GONE || btnPositive.getVisibility() == View.GONE) {
                viewLine.setVisibility(View.GONE);
            }

            if (positiveButtonColor != 0) {
                btnPositive.setTextColor(context.getResources().getColor(positiveButtonColor));
            }

            if (negativeButtonColor != 0) {
                btnNegative.setTextColor(context.getResources().getColor(negativeButtonColor));
            }

            commonDialog.setCancelable(canCancelable);

            return commonDialog;
        }

        public CommonDialog show() {
            final CommonDialog commonDialog = create();
            commonDialog.show();
            return commonDialog;
        }


    }
}
